<template>
  <!-- 产品添加 -->
  <div class="contractInfo box">
    <el-divider content-position="left">{{
      $route.query.mode == "add" ? "添加产品" : "编辑产品"
    }}</el-divider>
    <div class="formBox">
      <el-form
        :model="form"
        :rules="rules"
        ref="addForm"
        label-width="auto"
        label-position="right"
      >
        <el-form-item label="产品名称" prop="name">
          <el-input
            v-model="form.name"
            clearable
            placeholder="请输入产品名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="产品图片" style="width: 100%">
          <sc-upload-multiple v-model="form.vendorUrlList" :limit="5">
          </sc-upload-multiple>
        </el-form-item>
        <el-form-item label="规格型号" prop="specification" style="width: 100%">
          <el-input
            v-model="form.specification"
            type="textarea"
            rows="3"
            clearable
            placeholder="请输入产品规格型号"
          ></el-input>
        </el-form-item>
        <el-form-item label="材质" style="width: 100%">
          <el-input
            v-model="form.material"
            type="textarea"
            rows="3"
            clearable
            placeholder="请输入产品规格型号"
          ></el-input>
        </el-form-item>
        <el-form-item label="产品单价（元）" class="priceItem">
          <el-input-number
            v-model="form.onePrice"
            :precision="2"
            :step="1"
            clearable
            placeholder="请输入产品单价"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select
            v-model="form.unit"
            placeholder="请选择单位"
            style="width: 100%"
            remote
            filterable
            remote-show-suffix
            clearable
          >
            <el-option
              v-for="item in unitList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark" style="width: 100%">
          <el-input
            v-model="form.remark"
            rows="3"
            clearable
            type="textarea"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否是拆解件">
          <el-switch
            v-model="form.isDismantle"
            :inactive-value="0"
            :active-value="1"
          />
        </el-form-item>
        <el-form-item
          label="拆解件列表"
          style="width: 100%"
          v-show="form.isDismantle == 1"
        >
          <sc-form-table
            ref="innerTable"
            :modelValue="disassembleProductList"
            :addTemplate="productsTemplate"
            v-show="form.isDismantle == 1"
          >
            <el-table-column prop="name" label="拆解件名称" min-width="200">
              <template #default="scope">
                <el-input
                  v-model="scope.row.name"
                  placeholder="请输入拆解件名称"
                />
              </template>
            </el-table-column>
            <el-table-column prop="proportion" label="拆解比例" width="300">
              <template #default="scope">
                <el-input
                  v-model="scope.row.proportion"
                  placeholder="请输入拆解比例"
                />
              </template>
            </el-table-column>
            <el-table-column label="图片" min-width="300">
              <template #default="scope">
                <sc-upload-multiple
                  v-model="scope.row.vendorUrlList"
                  :limit="5"
                >
                </sc-upload-multiple>
              </template>
            </el-table-column>
          </sc-form-table>
        </el-form-item>
      </el-form>
      <div class="button">
        <el-button type="primary" @click="save" :loading="isSaveing"
          >保 存</el-button
        >
        <el-button @click="cencel">取 消</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { queryTypeByDic } from '@/utils/getData'
export default {
  name: 'vendorProductAdd',
  data () {
    return {
      visible: false,
      isSaveing: false,
      selectVendorLoading: false,
      //表单数据
      form: {
        name: '',//产品名称
        specification: '',//规格型号
        material: '',//材质
        onePrice: 0,//单价
        unit: '',//单位
        remark: '',//产品备注
        isDismantle: 0,//是否拆解件 0否 1是
        productsDismantleList: [],//拆解件
        vendorUrlList: []//	产品图片
      },
      disassembleProductList: [
        {
          name: '',
          proportion: '',//比例
          vendorUrlList: []//图片

        }
      ],//拆解产品
      //拆解产品模版
      productsTemplate: {
        name: '',
        proportion: '',//比例
        vendorUrlList: []//图片
      },
      vendorList: [],
      unitList: [],
      //验证规则
      rules: {
        name: [
          { required: true, message: '请输入产品名称' }
        ],
        onePrice: [
          { required: true, message: '请输入产品价格' }
        ],
        unit: [
          { required: true, message: '请选择单位', trigger: 'change' }
        ],
      }
    }
  },
  mounted () {
    this.selectdictionaryBy()
    if (this.$route.query.mode == 'edit') {
      this.getDetail()
    }
  },
  methods: {
    // 获取单位
    async selectdictionaryBy () {
      let result = await queryTypeByDic('unitCode')
      this.unitList = result
    },
    async getDetail () {
      let { data } = await this.$API.vendorProducts.getVendorProductDetail.get({ id: this.$route.query.id })
      this.form = data
      this.disassembleProductList = data.productsDismantleList
      console.log(this.form)
    },
    //表单提交方法
    save () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          this.form.productsDismantleList = this.disassembleProductList
          this.isSaveing = true
          let res = null
          let mode = this.$route.query.mode
          console.log(this.form)
          // return
          if (mode == 'add') {
            res = await this.$API.vendorProducts.addVendorProducts.post(this.form)
          } else if (mode == 'edit') {
            delete this.form.createTime
            delete this.form.updateTime
            res = await this.$API.vendorProducts.editVendorProducts.post(this.form)
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.closeSelectedTag(this.$route)
            this.$message.success("操作成功")
            this.$router.push({
              path: "/customer/vendorProducts",
            })
          }

        }
      })
    },
    // 取消
    cencel () {
      this.closeSelectedTag(this.$route)
      this.$router.push({
        path: "/customer/vendorProducts",
      })
    },
    //关闭tag
    closeSelectedTag (tag) {
      this.$store.commit("removeViewTags", tag)
      this.$store.commit("removeIframeList", tag)
      this.$store.commit("removeKeepLive", tag.name)
    },
  }
}
</script>
<style lang='scss' scoped>
.box {
  margin: 10px;
}

.formBox {
  padding: 30px;
  background: #fff;
  .el-form {
    width: 100%;
  }
}
.button {
  margin-top: 50px;
  padding-left: 60px;
}

:deep(.el-divider__text) {
  padding: 0 10px;
  font-size: 18px;
  // font-weight: 600;
  background-color: #f6f8f9;
}
</style>